<?php

/*
 * Following code will create a new product row
 * All product details are read from HTTP Post Request
 */

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['UserID']) && isset($_POST['Type']) && isset($_POST['Source']) && isset($_POST['Destination']) && isset($_POST['Over']) && isset($_POST['DepartureFrom']) && isset($_POST['Note'])) {
    
	$uid = $_POST['UserID'];
    $type = $_POST['Type'];
    $source = $_POST['Source'];
	$destination = $_POST['Destination'];
	$over = $_POST['Over'];
	$df = $_POST['DepartureFrom'];
	
	if(isset($_POST['DepartureTo']))
	{
		$dt  = "'" . $_POST['DepartureTo'] . "'";
	}
	else
	{
		$dt = "NULL";
	}
	
	$note = $_POST['Note'];

    // include db connect class
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();

    // mysql inserting a new row
    $result = mysql_query("INSERT INTO Offer(UserID, Type, Source, Destination, Over, DepartureFrom, DepartureTo, Note) VALUES($uid, '$type', '$source', '$destination', '$over', '$df', $dt, '$note')");

    // check if row inserted or not
    if ($result) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "Offer successfully created.";

        // echoing JSON response
        echo json_encode($response);
    } else {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";
        
        // echoing JSON response
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>